package org.ikross.twitter.session;

import java.io.InputStream;
import java.security.KeyStore;

import org.ikross.twitter.connector.ConnectorFactory;
import org.ikross.twitter.connector.IConnectorFactory;
import org.ikross.twitter.exception.IllegalTaskFactoryArgumentException;
import org.ikross.twitter.exception.TaskException;
import org.ikross.twitter.exception.TaskFactoryException;
import org.ikross.twitter.task.IConnector;

public class Session {
	
	private String userName;
	private KeyStore.PasswordProtection password;
	
	private String appName;
	
	Session(){
		this.userName = null;
		this.password = null;
		
		this.appName = "Kwitter";
	}
	
	public void setUserName(String userName){
		this.userName = userName;
	}
	
	public String getUserName(){
		return this.userName;
	}
	
	public void setPassword(KeyStore.PasswordProtection password){
		this.password = password;
	}
	
	public KeyStore.PasswordProtection getPassword(){
		return this.password;
	}
	
	/**
	 * @return the appName
	 */
	public String getAppName() {
		return appName;
	}

	/**
	 * @param appName the appName to set
	 */
	public void setAppName(String appName) {
		this.appName = appName;
	}
	
	public boolean isPreparedToAuthentication(){
		return (this.getUserName()!=null && this.getPassword()!=null);
	}

	public IConnectorFactory createTaskFactory(Class<ConnectorFactory> factoryType) throws IllegalTaskFactoryArgumentException, TaskFactoryException, InstantiationException, IllegalAccessException{
		IConnectorFactory taskFactory = factoryType.newInstance();
		taskFactory.setSession(this);
		
		return taskFactory;
	}
	
	public InputStream executeTask(IConnector task) throws TaskException{
		return task.execute();
	}
	
	public void destroy(){
		this.userName = "";
		this.userName = null;
		this.password = new KeyStore.PasswordProtection("".toCharArray());
		this.password = null;
	}
}